package com.example.airoles.controller;

import com.example.airoles.config.LlmConfig;
import com.example.airoles.service.LlmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

@RestController
@RequestMapping("/api/system")
public class SystemController {
    
    @Autowired(required = false)
    private LlmService llmService;
    
    @Autowired
    private LlmConfig llmConfig;
    
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "healthy");
        health.put("timestamp", java.time.LocalDateTime.now());
        health.put("llm_provider", llmConfig.getProvider());
        health.put("llm_service_available", llmService != null);
        
        return ResponseEntity.ok(health);
    }
    
    @GetMapping("/llm-info")
    public ResponseEntity<Map<String, Object>> getLlmInfo() {
        Map<String, Object> info = new HashMap<>();
        
        info.put("current_provider", llmConfig.getProvider());
        info.put("available_providers", List.of("qiniu"));
        info.put("service_loaded", llmService != null);
        
        if (llmConfig.isQiniu()) {
            info.put("provider_details", Map.of(
                "name", "Qiniu AI",
                "description", "七牛云AI大模型推理服务",
                "api_format", "OpenAI-compatible",
                "models", List.of("qwen-plus", "qwen-turbo", "qwen-max")
            ));
        } else {
            info.put("provider_details", Map.of(
                "name", "Unknown",
                "description", "Provider not configured",
                "api_format", "N/A",
                "models", List.of()
            ));
        }
        
        return ResponseEntity.ok(info);
    }
    
    @PostMapping("/test-llm")
    public ResponseEntity<Map<String, Object>> testLlm(@RequestBody Map<String, String> request) {
        Map<String, Object> result = new HashMap<>();
        
        if (llmService == null) {
            result.put("status", "error");
            result.put("message", "LLM service not available");
            return ResponseEntity.status(500).body(result);
        }
        
        try {
            String testPrompt = request.getOrDefault("prompt", "你好，请简单介绍一下你自己");
            
            // Create a simple test conversation
            List<com.example.airoles.dto.ChatRequest.Message> testConvo = new ArrayList<>();
            com.example.airoles.dto.ChatRequest.Message userMsg = new com.example.airoles.dto.ChatRequest.Message();
            userMsg.role = "user";
            userMsg.text = testPrompt;
            testConvo.add(userMsg);
            
            long startTime = System.currentTimeMillis();
            var response = llmService.chatWithRole(
                "你是一个AI助手，请用简洁友好的方式回答问题。",
                "",
                testConvo
            );
            long endTime = System.currentTimeMillis();
            
            result.put("status", "success");
            result.put("provider", llmConfig.getProvider());
            result.put("response", response.assistant);
            result.put("response_time_ms", endTime - startTime);
            result.put("timestamp", java.time.LocalDateTime.now());
            
        } catch (Exception e) {
            result.put("status", "error");
            result.put("provider", llmConfig.getProvider());
            result.put("error", e.getMessage());
            result.put("timestamp", java.time.LocalDateTime.now());
        }
        
        return ResponseEntity.ok(result);
    }
}